package com.hsd.app.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hsd.app.dao.IDataManageDao;
import com.hsd.app.model.HsdDssRelation;
import com.hsd.app.model.HsdDssinfo;
import com.hsd.app.model.HsdEffctRelation;
import com.hsd.app.model.HsdEffectinfo;
import com.hsd.app.service.IDataManageService;
import com.hsd.core.base.dao.IBaseDAO;
import com.hsd.core.base.service.impl.BaseService;
import com.hsd.core.cache.DictCache;
import com.hsd.core.page.Finder;
import com.hsd.core.page.entity.PageParams;
import com.hsd.core.page.entity.Pagination;
import com.hsd.survey.model.HsdModelQuestion;

 
@SuppressWarnings("unchecked")
@Service
public class DataManageServiceImpl extends BaseService implements IDataManageService{

	@Autowired
	private IDataManageDao dataManageDao;

	@Override
	public IBaseDAO getBaseDao() {
		// TODO Auto-generated method stub
		return this.dataManageDao;
	}
	@Override
	public Pagination loadImagesList(PageParams pageParams,String namestr,String keyword,String catagory){
		Finder finder = Finder.create("select r");
		finder.append(" from HsdImages r ");
		finder.append(" where 1=1");
		if(!StringUtils.isBlank(namestr)){
			finder.append(" and r.namestr like:namestr");
			finder.setParam("namestr", "%" + namestr + "%");
		} 
		if(!StringUtils.isBlank(keyword)){
			finder.append(" and r.keyword like:keyword");
			finder.setParam("keyword", "%" + keyword + "%");
		} 
		if(!StringUtils.isBlank(catagory)){
			finder.append(" and r.catagory like:catagory");
			finder.setParam("catagory", "%" + catagory + "%");
		}  
		finder.append(" order by r.opertime desc ");
		Pagination page = dataManageDao.find(finder, pageParams.getPageNo(), pageParams.getPageSize());
		
		return page;
	}
	
	@Override
	public Pagination searchImagesList(PageParams pageParams,String searchTxt){
		Finder finder = Finder.create("select r");
		finder.append(" from HsdImages r ");
		finder.append(" where 1=1");
		if(!StringUtils.isBlank(searchTxt)){
			finder.append(" and r.namestr like:namestr");
			finder.setParam("namestr", "%" + searchTxt + "%");
			finder.append(" or r.keyword like:keyword");
			finder.setParam("keyword", "%" + searchTxt + "%");
		} 
		finder.append(" order by r.opertime desc ");
		Pagination page = dataManageDao.find(finder, pageParams.getPageNo(), pageParams.getPageSize());
		
		return page;
	}
	
	@Override
	public Pagination loadHpList(PageParams pageParams, String hpname, String hptype) {
		Finder finder = Finder.create("select r");
		finder.append(" from HsdHpInfo r ");
		finder.append(" where youxiaobj=1");
		if(!StringUtils.isBlank(hpname)){
			finder.append(" and r.hpName like:hpname");
			finder.setParam("hpname", "%" + hpname + "%");
		} 
		if(!StringUtils.isBlank(hptype)){
			finder.append(" and r.type =").append(hptype); 
		} 
		finder.append(" order by r.id desc");
		Pagination page = dataManageDao.find(finder, pageParams.getPageNo(), pageParams.getPageSize());
		
		return page;
	}

	@Override
	public Pagination loadFbList(PageParams pageParams, String fbname) {
		Finder finder = Finder.create("select  s");
		finder.append(" from HsdFbInfo s");
		finder.append(" where youxiaobj=1");
		if(!StringUtils.isBlank(fbname)){
			finder.append(" and s.fbname like:fbname");
			finder.setParam("fbname", "%" + fbname + "%");
		}
		 
		Pagination page = dataManageDao.find(finder, pageParams.getPageNo(), pageParams.getPageSize());
		return page;
	}
	
	@Override
	public Pagination loadDssList(PageParams pageParams, Map paramMap) {
		Finder finder = Finder.create("select s");
		finder.append(" from HsdDssinfo   s");
		finder.append(" where youxiaobj=1");
		String dssname = (String)paramMap.get("dssname");
		String catagory = (String)paramMap.get("catagory");
		if(!StringUtils.isBlank(dssname)){
			finder.append(" and s.dssname like:dssname");
			finder.setParam("dssname", "%" + dssname + "%");
		}
		if(!StringUtils.isBlank(catagory)){
			finder.append(" and s.catagory =").append(catagory); 
		}
		Pagination page = dataManageDao.find(finder, pageParams.getPageNo(), pageParams.getPageSize());
		return page;
	}

	@Override
	public Pagination loadSynList(PageParams pageParams, String namestr) {
		Finder finder = Finder.create("select s");
		finder.append(" from  HsdSynNames s");
		finder.append(" where youxiaobj=1");
		if(!StringUtils.isBlank(namestr)){
			finder.append(" and s.namestr like:namestr");
			finder.setParam("namestr", "%" + namestr + "%");
		} 
		Pagination page = dataManageDao.find(finder, pageParams.getPageNo(), pageParams.getPageSize());
		return page;
	}

	@Override
	public Pagination loadMdList(PageParams pageParams, String mdname) {
		Finder finder = Finder.create("select s");
		finder.append(" from HsdMdInfo   s");
		finder.append(" where youxiaobj=1");
		if(!StringUtils.isBlank(mdname)){
			finder.append(" and s.mdName like:mdName");
			finder.setParam("mdName", "%" + mdname + "%");
		} 
		Pagination page = dataManageDao.find(finder, pageParams.getPageNo(), pageParams.getPageSize());
		return page;
	}

	@Override
	public Pagination loadEffectList(PageParams pageParams, String effectname) {
		Finder finder = Finder.create("select s");
		finder.append(" from HsdEffectinfo   s");
		finder.append(" where youxiaobj=1");
		if(!StringUtils.isBlank(effectname)){
			finder.append(" and s.effectname like:effectname");
			finder.setParam("effectname", "%" + effectname + "%");
		} 
		Pagination page = dataManageDao.find(finder, pageParams.getPageNo(), pageParams.getPageSize());
		return page;
	}
	
	@Override
	public String loadFilterEffects(PageParams pageParams,String hpid,String effectname){
		Pagination page = this.loadEffectList(pageParams,effectname);
		List<HsdEffctRelation> relationList = null;
		// 获取已经选中的功效
		if(!StringUtils.isBlank(hpid)){
			relationList = dataManageDao.findList("select ef from HsdEffctRelation ef where ef.type=1 and ef.rid =" + hpid);
		}
		
		// 数据转换
		List list = page.getList();
		JSONArray listJson = new JSONArray();
		if(null != list && list.size() != 0){
			for(Object object : list){
				JSONObject json = JSONObject.fromObject(object);
				json.put("checked", "0");
				if(null != relationList && relationList.size() != 0){
					for(HsdEffctRelation relation : relationList){
						if(relation.getEffectid() == json.getLong("id")){
							json.put("checked", "1");
							break;
						}
					}
				}
				json.put("type", DictCache.getInstance().getCodeName("[功效类别]", String.valueOf(((HsdEffectinfo)object).getType()), null));
				listJson.add(json);
			}
		}
		
		JSONObject pageJson = new JSONObject();
		pageJson.put("pagesize", page.getPageSize());
		pageJson.put("pageno", page.getPageNo());
		pageJson.put("list", listJson);
		System.out.println(pageJson.toString());
		return pageJson.toString();
	}
	
	@Override
	public void saveEffectRelation(String saveEffect,String deleteEffect){
		JSONArray saveArray = JSONArray.fromObject(saveEffect);
		JSONArray deleteArray = JSONArray.fromObject(deleteEffect);
		if(saveArray.size() != 0){ // 保存功效关系
			for(Object obj : saveArray){
				JSONObject json = JSONObject.fromObject(obj);
				HsdEffctRelation relation = (HsdEffctRelation)JSONObject.toBean(json,HsdEffctRelation.class);
				dataManageDao.save(relation);
			}
		}
		if(deleteArray.size() != 0){ // 删除功效
			for(Object id : deleteArray){
				HsdEffctRelation effectRlation = new HsdEffctRelation();
				effectRlation.setId(new Long((Integer)id));
				dataManageDao.delete(effectRlation);
			}
		}
	}
	
	/**
	 * 加载过滤功效信息
	 * @param pageParams
	 * @return
	 */
	@Override
	public String loadFilterDss(PageParams pageParams,String hpid,String dssname){
		Map pmap = new HashMap();
		pmap.put("dssname", dssname);
		Pagination page = this.loadDssList(pageParams,pmap);
		List<HsdDssRelation> relationList = null;
		// 获取已经选中的功效
		if(!StringUtils.isBlank(hpid)){
			relationList = dataManageDao.findList("select ef from HsdDssRelation ef where ef.rtype=1 and ef.rid =" + hpid);
		}
		// 数据转换
		List list = page.getList();
		JSONArray listJson = new JSONArray();
		if(null != list && list.size() != 0){
			for(Object object : list){
				JSONObject json = JSONObject.fromObject(object);
				json.put("checked", "0");
				json.put("type", "0");
				if(null != relationList && relationList.size() != 0){
					for(HsdDssRelation relation : relationList){
						if(relation.getDssid() == json.getLong("id")){
							json.put("checked", "1");
							json.put("type",relation.getType());
							break;
						}
					}
				}
				json.put("catagory", DictCache.getInstance().getCodeName("[病症证类别]", String.valueOf(((HsdDssinfo)object).getCatagory()), null));
				listJson.add(json);
			}
		}
		
		JSONObject pageJson = new JSONObject();
		pageJson.put("pagesize", page.getPageSize());
		pageJson.put("pageno", page.getPageNo());
		pageJson.put("list", listJson);
		System.out.println(pageJson.toString());
		return pageJson.toString();
	}
	/**
	 * 删除功效关系
	 * @param relation
	 * @return
	 */
	@Override
	public void deleteEffectRelation(HsdEffctRelation relation){
		if(null != relation.getEffectid() && null!= relation.getRid()){
			dataManageDao.deleteBeansByProperties(HsdEffctRelation.class, new String[]{"effectid","rid","type"} , new String[]{String.valueOf(relation.getEffectid()),String.valueOf(relation.getRid()),String.valueOf(relation.getType())});
		}
	}
	
	/**
	 * 删除病症关系
	 * @param relation
	 * @return
	 */
	public void deleteDssRelation(HsdDssRelation relation){
		if(null != relation.getDssid() && null!= relation.getRid()){
			dataManageDao.deleteBeansByProperties(HsdDssRelation.class, new String[]{"dssid","rid","rtype"} , new String[]{String.valueOf(relation.getDssid()),String.valueOf(relation.getRid()),String.valueOf(relation.getRtype())});
		}
	}
	
	/**
	 * 保存或更新功效方案
	 * @param saveEffect
	 * @param deleteEffect
	 * @return
	 */
	@Override
	public void saveDssRelation(String saveDss,String deleteDss){
		JSONArray saveArray = JSONArray.fromObject(saveDss);
		JSONArray deleteArray = JSONArray.fromObject(deleteDss);
		if(saveArray.size() != 0){ // 保存功效关系
			for(Object obj : saveArray){
				JSONObject json = JSONObject.fromObject(obj);
				HsdDssRelation relation = (HsdDssRelation)JSONObject.toBean(json,HsdDssRelation.class);
				dataManageDao.save(relation);
			}
		}
		if(deleteArray.size() != 0){ // 删除功效
			for(Object id : deleteArray){
				HsdDssRelation relation = new HsdDssRelation();
				relation.setId(new Long((Integer)id));
				dataManageDao.delete(relation);
			}
		}
	}
	
	@Override
	public JSONObject getFbDataByname(String fbname) {
		JSONObject fbData = null; 
		Finder finder = Finder.create("select s");
		finder.append(" from HsdFbInfo   s");
		finder.append(" where youxiaobj=1");
		if(!StringUtils.isBlank(fbname)){
			finder.append(" and s.fbname like:fbname");
			finder.setParam("dssname", "%" + fbname + "%");
		}
		List<HsdModelQuestion> fbList = this.dataManageDao.findList(finder.getOrigHql());
		fbData.put("datalist", JSONArray.fromObject(fbList));
		return fbData;
	}

	@Override
	public JSONObject getMdDataByname(String mdname) {
		JSONObject fbData = null; 
		Finder finder = Finder.create("select s");
		finder.append(" from HsdMdInfo   s");
		finder.append(" where youxiaobj=1");
		if(!StringUtils.isBlank(mdname)){
			finder.append(" and s.mdName like:mdname");
			finder.setParam("fbname", "%" + mdname + "%");
		}
		List<HsdModelQuestion> fbList = this.dataManageDao.findList(finder.getOrigHql());
		fbData.put("datalist", JSONArray.fromObject(fbList));
		return fbData;
	}

	@Override
	public JSONObject getDssDataByname(String dssname) {
		JSONObject fbData = null;
		 
		Finder finder = Finder.create("select s");
		finder.append(" from HsdDssinfo   s");
		finder.append(" where youxiaobj=1");
		if(!StringUtils.isBlank(dssname)){
			finder.append(" and s.dssname like:dssname");
			finder.setParam("dssname", "%" + dssname + "%");
		}
		List<HsdModelQuestion> fbList = this.dataManageDao.findList(finder.getOrigHql());
		fbData.put("datalist", JSONArray.fromObject(fbList));
		return fbData;
	}

	@Override
	public JSONObject getEffectDataByname(String effectname) {
		JSONObject fbData = null;
		 
		Finder finder = Finder.create("select s");
		finder.append(" from HsdEffectinfo   s");
		finder.append(" where youxiaobj=1");
		if(!StringUtils.isBlank(effectname)){
			finder.append(" and s.effectname like:effectname");
			finder.setParam("effectname", "%" + effectname + "%");
		}
		List<HsdModelQuestion> fbList = this.dataManageDao.findList(finder.getOrigHql());
		fbData.put("datalist", JSONArray.fromObject(fbList));
		return fbData;
	}
	 
}